const { staff_collection } = require('../config')
const pick = require('lodash/pick')
const { db } = require('../config')
const { command } = require('../config')
const _ = command
exports.main = async (event, context) => {
    const data = event;
    // 根据筛选条件查询员工
    filter = {};
    if (data.hasOwnProperty('name')) filter.name = new RegExp(data.name, "i");
    if (data.hasOwnProperty('status')) filter.status = data.status;
    if (data.hasOwnProperty('region')) filter.region = new RegExp(data.region, "i");
    if (data.hasOwnProperty('grid')) filter.grid = new RegExp(data.grid, "i");
    if (data.hasOwnProperty('position')) filter.position = new RegExp(data.position, "i");
    console.log('查询条件', filter)
    const collection = staff_collection;
    const countRs = await db.collection(collection).where(filter).count();
    console.log('countRs', countRs);
    page = data.page;
    pageSize = data.pageSize;
    console.log(44444444444, filter);
    const lists = await db.collection(collection)
        .where(filter)
        .skip(page * pageSize)
        .limit(pageSize)
        .get();
    // console.log('5555555', lists);
    let returnData = []
    if (lists.data && lists.data.length) {
        returnData = lists.data.map(i => {
            const item = pick(i, ['name', 'region', 'grid', 'account', 'id', 'status']);
            // console.log(item)
            return item;
        })
    }
    // console.log('lists',lists);
    return { total: countRs.total, data: returnData, page, pageSize };
}
